package com.axend.aerosense.common.connect.bean;

import java.util.HashMap;
import java.util.Map;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class MessageEntity {

    public static final byte[] MESSAGE_HEAD = new byte[]{(byte) 0xAA, (byte) 0xAA, 0x55, 0x55};

    private MsgTag tag;  //消息tag
    private int length;  //消息长度
    private byte[] body;  //消息主体
    private Object bodyObj;  //消息体对象

    public MessageEntity(MsgTag tag, byte[] body) {
        this.tag = tag;
        this.body = body;
    }

    public MessageEntity(MsgTag tag, Object bodyObj) {
        this.tag = tag;
        this.bodyObj = bodyObj;
    }

    public MessageEntity(MsgTag tag) {
        this.tag = tag;
    }

    @Getter
    public enum BodyState{
        setSuccess(1, new byte[]{0x01, 0x00, 0x00, 0x00}),  //设置设备参数成功
        setFailed(2, new byte[]{0x00, 0x00, 0x00, 0x00}),  //设置设备参数失败
        getMessage(3, new byte[]{0x00, 0x00, 0x00, 0x00})  //获取设备设置的参数
        ;

        private int state;
        private byte[] bytes;

        BodyState(int state, byte[] bytes) {
            this.state = state;
            this.bytes = bytes;
        }
    }

    @Getter
    public enum MsgTag{

        getID(2, new byte[]{0x00, 0x02}),  //获取雷达ID
        setWorkDistance(3, new byte[]{0x00, 0x03}),  //设置工作距离
        getWorkDistance(4, new byte[]{0x00, 0x04}),  //获取工作距离
        setAlarmDelay(5, new byte[]{0x00, 0x05}),  //设置报警延时
        getAlarmDelay(6, new byte[]{0x00, 0x06}),  //获取报警延时
        switchSTAMode(7, new byte[]{0x00, 0x07}),  //切换到STA模式
        setFamilyWifiInfo(10, new byte[]{0x00, 0x0A}),  //设置家庭Wifi信息
        getHardwareVersion(11, new byte[]{0x00, 0x0B}),  //获取设备固件版本信息
        setDeviceIPAndPort(12, new byte[]{0x00, 0x0C}),  //设置IP与端口
        setDeviceHeight(25, new byte[]{0x00, 0x19}),  //设置设备安装高度
        getDeviceHeight(26, new byte[]{0x00, 0x1A}),  //获取设备安装高度
        ;

        private int tag;
        private byte[] bytes;
        @Getter(AccessLevel.NONE)
        private static Map<Integer, MsgTag> map;

        static {
            map = new HashMap<>();
            for (MsgTag m : MsgTag.values()){
                map.put(m.getTag(), m);
            }
        }

        MsgTag(int tag, byte[] bytes) {
            this.tag = tag;
            this.bytes = bytes;
        }

        public static MsgTag get(int tag){
            return map == null ? null : map.get(tag);
        }

    }
}
